/*
 * Copyright (C) 2015 备胎金服
 * 未经授权允许不得进行拷贝和修改
 *   http://www.btjf.com/
 */
package com.zhangman.androidgame;

import android.util.Log;
import com.zhangman.framework.Screen;
import com.zhangman.framework.implementation.AndroidGame;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by zhangman on 2016/11/1 14:36.
 * Email: zhangman523@126.com
 */
public class SampleGame extends AndroidGame {

  public static String map;
  boolean firstTimeCreate = true;

  @Override public Screen getInitScreen() {
    if (firstTimeCreate) {
      Assets.load(this);
      firstTimeCreate = false;
    }
    InputStream is = getResources().openRawResource(R.raw.map1);
    map = convertStreamToString(is);
    return new SplashLoadingScreen(this);
  }

  private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
      while ((line = reader.readLine()) != null) {
        sb.append((line + "\n"));
      }
    } catch (IOException e) {
      Log.w("SampleGame", e.getMessage());
    } finally {
      try {
        is.close();
      } catch (IOException e) {
        Log.w("LOG", e.getMessage());
      }
    }
    return sb.toString();
  }

  @Override protected void onResume() {
    super.onResume();
    Assets.theme.play();
  }

  @Override protected void onPause() {
    super.onPause();
    Assets.theme.pause();
  }

  @Override public void onBackPressed() {
    getCurrentScreen().backButton();
  }
}
